package com.feng.model
{
	import com.feng.data.UpgradeType;
	import com.feng.debug.logger;

	public class Player
	{
		public var id:int;

		private var _hp:int;

		/**
		 * 攻击力
		 */
		public var attack:int;

		/**
		 * 防御
		 */
		public var defense:int;

		/**
		 * 速度
		 */
		public var speed:int;

		/**
		 * 最低伤害
		 */
		public static const MIN_DAMAGE:int = 5;

		public function Player(obj:*)
		{
			this.id = obj.id;
			this.hp = obj.hp;
			this.attack = obj.atk;
			this.defense = obj.def;
			this.speed = obj.spd;
		}

		/**
		 * 血量
		 */
		public function get hp():int
		{
			return _hp;
		}

		/**
		 * @private
		 */
		public function set hp(value:int):void
		{
			_hp = value;
			if (_hp < 0)
				_hp = 0;
		}

		/**
		 * 复制该玩家
		 * @return 该玩家复制品
		 */
		public function copy():Player
		{
			var newPlayer:Player = new Player({});
			newPlayer.id = this.id;
			newPlayer.hp = this.hp;
			newPlayer.attack = this.attack;
			newPlayer.defense = this.defense;
			newPlayer.speed = this.speed;
			return newPlayer;
		}

		/**
		 * 是否死亡
		 */
		public function get isDie():Boolean
		{
			return hp == 0;
		}

		/**
		 * 判断能否杀手玩家
		 * @param player 受攻击玩家
		 * @return
		 */
		public function canKill(player:Player):Boolean
		{
			//可造成伤害
			var damage:int = attackDamage(player);
			return damage >= player.hp;
		}

		/**
		 * 获取攻击玩家可造成的伤害
		 * @param player 受攻击玩家
		 * @return 伤害值
		 *
		 */
		public function attackDamage(player:Player):int
		{
			//5、伤害计算公式是“攻击减防御”，但最小伤害不低于5点。
			var damage:int = this.attack - player.defense;
			if (damage < MIN_DAMAGE)
			{
				damage = MIN_DAMAGE;
			}
			return damage;
		}

		/**
		 * 提升属性
		 * @param upgradeType 提升类型
		 * @return 提升属性后的新玩家
		 *
		 */
		public function upgrade(upgradeType:int):Player
		{
			var newPlayer:Player = this.copy();
			switch (upgradeType)
			{
				case UpgradeType.ADD_ATTACK:
					newPlayer.attack += 4;
					break;
				case UpgradeType.ADD_DEFENSE:
					newPlayer.defense += 3;
					break;
				case UpgradeType.ADD_SPEED:
					newPlayer.speed += 2;
					break;
				case UpgradeType.ADD_ALL:
					newPlayer.attack++;
					newPlayer.defense++;
					newPlayer.speed++;
					break;
				default:
					logger("脑子进水了，居然想提升没有的属性！");
					break;
			}
			return newPlayer;
		}

		/**
		 * 单挑该玩家
		 * @param player 受挑战玩家
		 * @return 单挑后剩余血量
		 */
//		public function challenge(player:Player):Number
//		{
//			var me:Player = this.copy();
//			var enemy:Player = player.copy();
//
//			//速度快的玩家
//			var fastPlayer:Player = me.speed > enemy.speed ? me : enemy;
//			//速度慢的玩家
//			var slowPlayer:Player = me.speed > enemy.speed ? enemy : me;
//			while (fastPlayer.hp > 0 && slowPlayer.hp > 0)
//			{
//				//如果俩玩家速度相同
//				if (fastPlayer.speed == slowPlayer.speed)
//				{
//					slowPlayer.hp -= fastPlayer.attackDamage(slowPlayer);
//					fastPlayer.hp -= slowPlayer.attackDamage(fastPlayer);
//				}
//				else
//				{
//					slowPlayer.hp -= fastPlayer.attackDamage(slowPlayer) * 2;
//					//死了是没法攻击的
//					if (slowPlayer.hp > 0)
//					{
//						fastPlayer.hp -= slowPlayer.attackDamage(fastPlayer);
//					}
//				}
//			}
//			return me.hp - enemy.hp;
//		}
	}
}
